home *** CD-ROM | disk | FTP | other *** search
- unit MailU;
-
- interface
-
- {$IFDEF WIN32}
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, DB, DBTables, ExtCtrls, WPStatus, Wpstat2, WpWinCtr, WPRich,
- WPTbar, Buttons, WPDEFS, ComCtrls, Tabnotbk, WPRuler, Menus;
- {$ELSE}
- uses
- WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, DB, DBTables, ExtCtrls, WPStatus, Wpstat2, WpWinCtr, WPRich,
- WPTbar, Buttons, WPDEFS, Tabnotbk, WPRuler, Menus;
- {$ENDIF}
-
- type
- TForm1 = class(TForm)
- Panel2: TPanel;
- Button1: TButton;
- Bevel1: TBevel;
- FieldList: TListBox;
- Table1: TTable;
- OpenDialog1: TOpenDialog;
- Label1: TLabel;
- WPToolBar1: TWPToolBar;
- Button2: TButton;
- MakeInsert: TBitBtn;
- Bevel2: TBevel;
- Label2: TLabel;
- Next: TSpeedButton;
- Prev: TSpeedButton;
- InsDate: TBitBtn;
- TabbedNotebook1: TTabbedNotebook;
- AllRtfText: TWPRichText;
- WPRichText1: TWPRichText;
- WPRuler1: TWPRuler;
- MergeAll: TBitBtn;
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- LoadForm1: TMenuItem;
- SaveForm1: TMenuItem;
- N1: TMenuItem;
- SaveMergedText1: TMenuItem;
- N2: TMenuItem;
- Exit1: TMenuItem;
- WPAltStatusBar1: TWPAltStatusBar;
- Example1: TMenuItem;
- addtablerowwith3columns1: TMenuItem;
- Table3columns1: TMenuItem;
- N6columns1: TMenuItem;
- BitBtn1: TBitBtn;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure FieldListClick(Sender: TObject);
- procedure MakeInsertClick(Sender: TObject);
- procedure PrevClick(Sender: TObject);
- procedure NextClick(Sender: TObject);
- procedure WPRichText1GetTextToInsert(Sender: TObject; var p: PChar;
- text: PChar; LenOfAutoText: Integer; tag: Word; c: Char;
- var DoContinue: Boolean);
- procedure InsDateClick(Sender: TObject);
- procedure WPToolBar1IconSelection(Sender: TObject; var Typ: TWpSelNr;
- const str: string; const group, num, index: Integer);
- procedure FormCreate(Sender: TObject);
- procedure MergeAllClick(Sender: TObject);
- procedure LoadForm1Click(Sender: TObject);
- procedure SaveForm1Click(Sender: TObject);
- procedure SaveMergedText1Click(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure addtablerowwith3columns1Click(Sender: TObject);
- procedure Table3columns1Click(Sender: TObject);
- procedure N6columns1Click(Sender: TObject);
- procedure WPRichText1ProtectedChange(Sender: TObject; text: string;
- Tag: Word);
- procedure BitBtn1Click(Sender: TObject);
- protected
- procedure WMGetMinMaxInfo(var MSG: Tmessage); message WM_GetMinMaxInfo;
- public
- { static data to be used for mailmerging }
- {$IFNDEF WIN32}
- textbuff : array[0..1024] of Char;
- {$ELSE}
- textbuff : string; { Delphis huge string }
- {$ENDIF}
- MinWidth : Integer;
- MinHeight : Integer;
- DidMerge : Boolean;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.WMGetMinMaxInfo(var MSG: Tmessage);
- Begin
- inherited;
- with PMinMaxInfo(MSG.lparam)^ do
- begin
- with ptMinTrackSize do
- begin
- X := MinWidth;
- Y := MinHeight;
- end;
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- MinWidth := 350;
- MinHeight := Height;
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- var
- nam : string;
- begin
- if OpenDialog1.Execute then
- begin
- Table1.Active := FALSE;
- nam := OpenDialog1.FileName;
- Table1.DataBaseName :=
- ExtractFilePath(nam);
- Table1.TableName :=
- ExtractFileName(nam);
- try
- Label1.Caption := nam;
- Table1.Active := TRUE;
- Table1.GetFieldNames(FieldList.Items);
- Prev.Enabled := TRUE;
- Next.Enabled := TRUE;
- except
- Label1.Caption := 'cannot open';
- end;
- end;
- end;
-
- { Switch between Viewmode and Editmode.
- The MailMerge variables (InsertPoints)
- are hidden in Viewmode.
- Note: If you save the text in Viewmode
- the InsertPoints are not saved.
-
- You can save the Text without the inserted
- Text when HideAutomatic = TRUE
- }
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- if not WPRichText1.Readonly then
- begin
- Button2.Caption := 'Viewmode OFF';
- WPRichText1.Readonly := TRUE;
- WPRichText1.HideInsertPoints := TRUE;
- end else
- begin
- Button2.Caption := 'Editmode OFF';
- WPRichText1.Readonly := FALSE;
- WPRichText1.HideInsertPoints := FALSE;
- end;
- end;
-
- procedure TForm1.FieldListClick(Sender: TObject);
- begin
- MakeInsert.Enabled := (FieldList.ItemIndex>=0)
- and not WPrichText1.Readonly and
- (FieldList.Items.Count>0);
- end;
-
-
- procedure TForm1.MakeInsertClick(Sender: TObject);
- var
- i : Integer;
- begin
- i := FieldList.ItemIndex;
- if (i>=0) and not WprichText1.ReadOnly
- and (TabbedNotebook1.PageIndex=0) then
- begin
- { Make an insertpoint:
- InputInsertpoint(c : Char;tag : Word;text : string);
- c = any character. for example '#'
- tag = 1..65535 will be used to distinguish between the points
- text will be merged at once
- }
- WPRichText1.InputInsertPoint('#',i+1,'['+FieldList.Items[i]+']');
- if WPRichText1.Visible then WPRichText1.SetFocus;
- end;
- end;
-
- procedure TForm1.InsDateClick(Sender: TObject);
- var
- a,aa : TAttr;
- begin
- if not WprichText1.ReadOnly then
- begin
- { Make an insertpoint:
- InputInsertpoint(c : Char;tag : Word;text : string);
- c = any character. for example '#'
- tag = 1..65535 will be used to distinguish between the points
- text will be merged at once
- }
- a := WPRichText1.Attr;
- aa := a;
- { set the backgrund color }
- a.Color := a.Color + (2 * 16);
- WPRichText1.Attr := a;
- WPRichText1.InputInsertPoint('*',1000,'[DATE]');
- WPRichText1.SetFocus;
- WPRichText1.Attr := aa;
- end;
- end;
-
- { This procedure will be executed when WPRichText1.MergeText was
- called up. It has to fill in the data which should be inserted.
- You should never call any WPRichText procedure within
- the GetTextToInsert Eventhandler!
- var p: PChar; This pointer has to be nil or should
- point to the pchar which has to be inserted.
- (attention: dont use local arrays)
- text: PChar; The pointer let you know about the text
- which follows the insertpoint. You vat all text
- until lineend by reading this varible. It can be
- used to do some calculation or to fill in the
- previous data. (the lenght is then LenOfAutoText)
- LenOfAutoText: Integer;
- If you want to use the previous data this will give
- it to you as a string:
- Copy(StrPas(text),1,LenOfAutoText);
- tag: Word; The second important varaiable: The tag of the
- insertpoint.
- c: Char; Maybe of some use: The Character which shows
- (colored in red) the insertpoint
- var DoContinue: Boolean
- If you assign FALSE to DoContinue, the
- merging will be stoped.
- }
- procedure TForm1.WPRichText1GetTextToInsert(Sender: TObject; var p: PChar;
- text: PChar; LenOfAutoText: Integer; tag: Word; c: Char;
- var DoContinue: Boolean);
- var
- field : TField;
- begin
- if Tag=1000 then { Insert today date }
- begin
- {$IFDEF WIN32}
- TextBuff := DateToStr(Date);
- p := PChar(TextBuff);
- {$ELSE}
- StrPLCopy(textbuff,DateToStr(Date),200);
- p := @(textbuff[0]);
- {$ENDIF}
- end
- else if (Tag>0) and (Tag<=FieldList.Items.Count) and Table1.Active then
- begin
- field := Table1.FieldByName(FieldList.Items.Strings[tag-1]);
- if field<>nil then
- begin
- {$IFDEF WIN32}
- TextBuff := field.AsString;;
- p := Pchar(TextBuff);
- {$ELSE}
- StrPLCopy(textbuff, field.AsString, 1022);
- p := @(textbuff[0]);
- {$ENDIF}
- end;
- end;
- end;
-
- procedure TForm1.PrevClick(Sender: TObject);
- var
- s : TMemoryStream;
- old : Boolean;
- begin
- if Table1.Active then
- begin
- Table1.Prior;
- Prev.Enabled := not Table1.BOF;
- Next.Enabled := not Table1.EOF;
- MergeAll.Enabled := not Table1.EOF;
- WPRichtext1.MergeText; { if the CursorPos should not change you may use MergeTextFor(#0,0); }
- try
- s := TMemoryStream.Create;
- old := WPRichText1.HideInsertPoints;
- WPRichText1.HideInsertPoints := TRUE;
- WPRichText1.SaveToStream(s);
- WPRichText1.HideInsertPoints := old;
- s.Position := 0;
- AllRtfText.CPPosition := $FFFFFF;
- AllRtfText.LoadFromStream(s);
- DidMerge := TRUE;
- finally
- s.Free;
- end;
- end;
- end;
-
- procedure TForm1.NextClick(Sender: TObject);
- var
- s : TMemoryStream;
- old : Boolean;
- begin
- if Table1.Active then
- begin
- Table1.Next;
- Prev.Enabled := not Table1.BOF;
- Next.Enabled := not Table1.EOF;
- MergeAll.Enabled := not Table1.EOF;
- WPRichtext1.MergeText;
- try
- s := TMemoryStream.Create;
- old := WPRichText1.HideInsertPoints;
- WPRichText1.HideInsertPoints := TRUE;
- WPRichText1.SaveToStream(s);
- WPRichText1.HideInsertPoints := old;
- s.Position := 0;
- AllRtfText.CPPosition := $FFFFFF;
- AllRtfText.LoadFromStream(s);
- DidMerge := TRUE;
- finally
- s.Free;
- end;
- end;
- end;
-
- { please include WPDEFS to the usage }
- procedure TForm1.WPToolBar1IconSelection(Sender: TObject;
- var Typ: TWpSelNr; const str: string; const group, num, index: Integer);
- begin
- if typ=wptIconSel then
- begin
- if group=WPI_GR_DISK then
- begin if num=WPI_CO_NEW then
- begin
- if TabbedNotebook1.PageIndex=0 then
- begin
- WPRichText1.Clear;
- WPRichText1.CPPosition := 0;
- end else
- begin
- AllRtfText.Clear;
- AllRtfText.CPPosition := 0;
- if Table1.Active then
- begin Table1.First;
- MergeAll.Enabled := TRUE;
- Next.Enabled := TRUE;
- Prev.Enabled := FALSE;
- end;
- end;
- WPToolBar1.SelectIcon(index,group,num);
- end
- else if num= WPI_CO_EXIT then Close;
- end;
- end else
- if typ=wptIconDeSel then
- begin
-
- end;
- end;
-
-
- procedure TForm1.MergeAllClick(Sender: TObject);
- var
- old : Boolean;
- var i : Longint;
- begin
- i := 0;
- if not Table1.Active then exit;
- AllRtfText.HideInsertPoints := TRUE;
- AllRtfText.FastCopyProperties(WPRichText1);
- if MessageBox(0,'Merge all Records in Database?',
- 'MailMerge',IDOK)=IDOK then
- try
- while not Table1.EOF do
- begin
- inc(i);
- WPAltStatusBar1.SetString(stStatus,IntToStr(i));
- Table1.Next;
- if not Table1.EOF then
- begin
- WPRichtext1.FastMergeText;
- AllRtfText.Memo.FastAppendText(WPRichText1.Memo.FirstPar);
- end;
- end;
- finally
- { After usage of FastAppendText it is neccessary to call Refresh }
- AllRtfText.Refresh;
- { the usage of FastMergeText makes it necessary to call Refresh }
- WPRichtext1.Refresh;
-
- Prev.Enabled := not Table1.BOF;
- Next.Enabled := not Table1.EOF;
- MergeAll.Enabled := not Table1.EOF;
- DidMerge := TRUE;
- end;
- end;
-
- procedure TForm1.LoadForm1Click(Sender: TObject);
- begin
- WPRichText1.Load;
- end;
-
- procedure TForm1.SaveForm1Click(Sender: TObject);
- var
- old : Boolean;
- begin
- old := WPRichText1.HideInsertPoints;
- { WPRichText1.HideInsertPoints has to be FALSE. Otherwise
- the insertpoints won't be saved }
- WPRichText1.HideInsertPoints := FALSE;
- WPRichText1.SaveAs;
- WPRichText1.HideInsertPoints := old;
- end;
-
- procedure TForm1.SaveMergedText1Click(Sender: TObject);
- begin
- AllRtfText.SaveAs;
- end;
-
- procedure TForm1.Exit1Click(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TForm1.addtablerowwith3columns1Click(Sender: TObject);
- var
- CWidth : array[1..5] of Integer;
- const
- mult = 255 div 6;
- begin
- WPRichText1.Clear;
- CWidth[1] := mult;
- CWidth[2] := mult * 2;
- CWidth[3] := mult * 3;
- WPRichText1.CreateTable(1,3,@Cwidth[1],FALSE);
- end;
-
- procedure TForm1.Table3columns1Click(Sender: TObject);
- var
- CWidth : array[1..5] of Integer;
- const
- mult = 255 div 6;
- begin
- WPRichText1.Clear;
- CWidth[1] := mult;
- CWidth[2] := mult * 2;
- CWidth[3] := mult * 3;
- WPRichText1.CreateTable(1,3,@Cwidth[1],TRUE);
- end;
-
- procedure TForm1.N6columns1Click(Sender: TObject);
- begin
- WPRichText1.Clear;
- WPRichText1.CreateTable(1,6,nil,TRUE);
- end;
-
- { property ProtectedProp = [ppIsInsertpoint,ppAutomatic] !
- otherwise this event will not be executed }
- procedure TForm1.WPRichText1ProtectedChange(Sender: TObject; text: string;
- Tag: Word);
- var
- newstr : String;
- c : Char;
- field : TField;
- fieldname : string;
- begin
- if text='' then c:=#0 else c := text[1];
- if c = '*' then
- begin
- Application.MessageBox('Cannot change DATE','ProtectedChange - Event',0);
- exit;
- end;
-
- if DidMerge and Table1.Active and (Tag>0) then
- begin
- fieldname := FieldList.Items.Strings[tag-1];
- field := Table1.FieldByName(fieldname);
- if field<>nil then
- begin
- if Field is TStringField then { Update a Datafield }
- begin
- newstr := Field.AsString;
- if InputQuery('ProtectedChange - Event',
- 'Change Datafield [' + fieldname + ']: "' + text+'"',
- newstr) then
- begin
- Table1.Edit;
- Field.AsString := newStr;
- Table1.Post;
- WPRichText1.MergeTextFor('#', tag);
- end;
- end else
- begin
- Application.MessageBox('Cannot edit this Fieldtype','ProtectedChange - Event',0);
- end;
- end;
- end else Application.MessageBox('Please merge data first.','ProtectedChange - Event',0);
- end;
-
- procedure TForm1.BitBtn1Click(Sender: TObject);
- begin
- if not WprichText1.ReadOnly then
- begin
- WPRichText1.InputText(#12+#0);
- WPRichText1.SetFocus;
- end;
- end;
-
- end.
-